package com.liukai.novel_rack.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_rack.R;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;

import java.util.ArrayList;
import java.util.List;

public class HotAdapter extends RecyclerView.Adapter<HotAdapter.HotViewHolder> {
    private Context mContext;
    private List<NovelInfo> hotNovelList = new ArrayList<>();

    public HotAdapter(Context context) {
        this.mContext = context;
        hotNovelList = Constans.getHotNovel();
    }

    @NonNull
    @Override
    public HotViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_hot, null);
        return new HotViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HotViewHolder holder, final int position) {
        holder.mTvIndex.setText((position + 1) + "");
        holder.mTvNovelName.setText(hotNovelList.get(position).getNovelName());
        switch (position) {
            case 0:
                holder.mTvIndex.setBackgroundColor(mContext.getResources().getColor(R.color.colorHotOne));
                break;
            case 1:
                holder.mTvIndex.setBackgroundColor(mContext.getResources().getColor(R.color.colorHotTwo));
                break;
            case 2:
                holder.mTvIndex.setBackgroundColor(mContext.getResources().getColor(R.color.colorHotThree));
                break;
            default:
                holder.mTvIndex.setBackgroundColor(mContext.getResources().getColor(R.color.colorGray));
                break;
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnSearchItemListener.onItemClick(hotNovelList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return hotNovelList.size();
    }

    class HotViewHolder extends RecyclerView.ViewHolder {

        public TextView mTvNovelName;
        public TextView mTvIndex;

        public HotViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvIndex = itemView.findViewById(R.id.tv_index);
            mTvNovelName = itemView.findViewById(R.id.novel_name);

        }
    }

    private OnSearchItemListener mOnSearchItemListener;

    public void setOnSearchItemListener(OnSearchItemListener onSearchItemListener) {
        mOnSearchItemListener = onSearchItemListener;
    }
}
